<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button icon="el-icon-plus" type="primary" @click="open_dialog" />
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-select v-model="reqParams.equipName" clearable placeholder="警报设备">
						<el-option
							v-for="item in alarmEquipNameOptions"
							:key="item.code"
							:label="item.name"
							:value="item.code" />
					</el-select>
					<el-button icon="el-icon-search" type="primary" @click="search" />
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :api-obj="apiObj" :column="column" row-key="id" hideContextMenu>
				<!-- 固定列-选择列 -->
				<el-table-column label="#" type="index" width="40"></el-table-column>
				<el-table-column align="center" fixed="right" label="操作" width="140">
					<template #default="scope">
						<el-button text type="primary" size="small" @click="open_dialog(scope.row)"> 编辑 </el-button>
						<el-divider direction="vertical" />
						<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button text type="primary" size="small">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>

				<template #level="{ data }">
					<el-tag disable-transitions :type="tagLevel(data.level)">
						{{ data.levelDesc }}
					</el-tag>
				</template>
			</scTable>
		</el-main>

		<modify ref="modify" @complete="complete" />
	</el-container>
</template>

<script>
import modify from "./modify";

export default {
	components: {
		modify,
	},
	data() {
		return {
			apiObj: this.$API.equipalarmitem.page,
			reqParams: {
				equipName: '',
			},
			alarmEquipNameOptions: [],
			column: [
				{ prop: "id", label: "id", width: 100, hide: true },
				{ prop: 'equipName', label: '设备', width: 80 },
				{ prop: 'pos', label: '顺序号', width: 80 },
				{ prop: 'content', label: '警报内容', width: 160 },
				{ prop: 'level', label: '警报等级', width: 100 },
				{ prop: 'classify', label: '警报分类', width: 100 },
				{ prop: 'createTime', label: '创建时间', width: 100 },
			],
		};
	},
	mounted() {
		this.init();
	},
	methods: {
		async init() {
			const resp2 = await this.$API.syscodetype.bycode.get('mes_alarm_equipment');
			this.alarmEquipNameOptions = resp2.data.codes.map(s => ({ name: s.name, code: s.codeValues }));
		},
		complete() {
			this.$refs.table.refresh();
		},
		search() {
			this.$refs.table.upData(this.reqParams);
		},
		tagLevel(level) {
			const types = ['', 'danger', 'warning', 'info', 'success', ''];
			return types[level];
		},
		async table_del(row) {
			let resp = await this.$API.equipalarmitem.delete.delete([row.id]);
			if (resp.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(resp.message, "提示", { type: "error" });
			}
		},
		open_dialog(row) {
			if (row.id) {
				this.$refs.modify.open(row);
			} else {
				this.$refs.modify.open();
			}
		},
	},
};
</script>
